<script>
import { captchaImageFn, sendSmsCaptchaFn, passportLoginFn } from '@/api/login'

export default {
  // 一登录就需要获取图像验证码，点击倒计时的时候千万要注意还原timer的值，例如倒计时结束需要将timer恢复到null，离开该页面是也需要清除掉定时器
  name: 'LoginIndex',
  data() {
    return {
      picCode: '',//用户输入的图形验证码
      picKey: '',//请求传递的图像验证码唯一标识
      picUrl: '',//存储请求渲染的图片地址
      second: 5,//当前秒数
      totalSecond: 5,//总秒数
      timer: null,//定时器id
      mobile: '',//手机号
      smsCode: '',//短信验证码

    }
  },
  methods: {
    loginFn() {
      if (!this.validFn()) return
      if (!/^\d{6}$/.test(this.smsCode)) {
        showToast('请输入正确的短信验证码')
        return
      }
      passportLoginFn(this.mobile, this.smsCode).then(res => {
        // console.log(res.data);
        // this.$store.commit("模块名字/方法名",传的参数)
        this.$store.commit('user/setUserInfo', res.data)

        const url = this.$route.query.backUrl || '/'
        this.$router.replace(url)


      })


    },
    getPicCode() {
      captchaImageFn().then(res => {
        // console.log(res);

        const { data: { base64, key } } = res
        this.picUrl = base64
        this.picKey = key
        showToast(res.message)
      })
    },
    getCode() {
      if (!this.validFn()) {

        return
      }
      if (!this.timer && this.second === this.totalSecond) {
        sendSmsCaptchaFn(this.picCode, this.picKey, this.mobile).then((res) => {
          if (res.status === 200) {
            showToast('短信验证码发送成功')
          }

        })
        this.timer = setInterval(() => {
          // console.log('我还没有结束');

          this.second--
          if (this.second < 1) {
            clearInterval(this.timer)
            this.timer = null
            this.second = this.totalSecond


          }
        }, 1000)
      }

    },
    // 点击倒计时的时候需要校验手机号和图像验证码是否合法 方法
    validFn() {
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        showToast('请输入正确的手机号')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        showToast('请输入正确的图形验证码')

        return false
      }
      return true
    }
  },
  mounted() {
    this.getPicCode()
  },
  unmounted() {
    clearInterval(this.timer)
  }

}
</script>

<template>
  <div class="login">
    <!-- 头部 -->
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.back()" />
    <!-- 主体 -->
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>
      <div class="form">

        <div class="form-item">
          <input type="text" class="inp" v-model.trim="mobile" placeholder="请输入手机号码">
        </div>
        <div class="form-item">
          <input type="text" class="inp" maxlength="5" v-model="picCode" placeholder="请输入图形验证码">
          <img v-show="picUrl" :src="picUrl" alt="" @click="getPicCode">
          <!-- 图片随便找 -->
        </div>
        <div class="form-item">
          <input type="text" class="inp" maxlength="6" v-model="smsCode" placeholder="请输入短信验证码">
          <button @click="getCode">{{ second === totalSecond ? '发送验证码' : `${second}秒后重新发送` }}</button>
        </div>

      </div>

      <div class="login-btn" @click="loginFn">登录</div>
    </div>




  </div>
</template>

<style lang="scss" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;

    h3 {
      font-size: 26px;
      font-weight: normal;

    }

    p {
      font-size: 26px;
      margin-top: 10px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form {
    .form-item {
      border-bottom: 1px solid #f3f1f2;
      padding: 8px;
      margin-bottom: 14px;
      display: flex;
      align-items: center;

      .inp {
        display: block;
        border: none;
        outline: none;
        height: 32px;
        font-size: 14px;
        flex: 1;
      }

      img {
        width: 94px;
        height: 31px
      }

      button {
        height: 31px;
        border: none;
        font-size: 13px;
        color: #cea26a;
        background-color: transparent;
        padding-right: 9px;

      }
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg, #ecb53c, #ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0, 0, 0, .1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>